package org.yzhang3.data.utils;

import org.yzhang3.data.common.Constants;

public class StringUtils {

	public static String format(int val, int w) {
		return format(Integer.toString(val), w);
	}
	
	/**
	 * format <code>target</code> to specific <code>length</code>. <br>
	 * &nbsp; &nbsp; 1) <code>length</code> &gt; 0, append to the head of <code>target</code>.<br>
	 * &nbsp; &nbsp; 2) <code>length</code> &lt; 0, append to the end of <code>target</code>.
	 * @param target
	 * @param len
	 * @return
	 */
	public static String format(String target, int length) {
		int n = Math.abs(length) - target.length();
		
		if (n <= 0) {
			return target;
		}
		
		StringBuilder sb = new StringBuilder(Math.abs(length));
		
		if (length < 0) {
			sb.append(target);
			for (int i=0; i<n; i++) {
				sb.append(Constants.BINARY_ZERO);
			}
		} else {
			for (int i=0; i<n; i++) {
				sb.append(Constants.BINARY_ZERO);
			}
			sb.append(target);
		}
		return sb.toString();
	}
	
}
